<script>
	import city from '@/utils/city'
	export default {
		// 此处globalData为了演示其作用，不是uView框架的一部分
		globalData: {
			isIphoneX: false	// 判断机型是否是iphoneX系列
		},
		onLaunch() {
			// 1.1.0版本之前关于http拦截器代码，已平滑移动到/common/http.interceptor.js中
			// 注意，需要在/main.js中实例化Vue之后引入如下(详见文档说明)：
			// import httpInterceptor from '@/common/http.interceptor.js'
			// Vue.use(httpInterceptor, app)
			// process.env.VUE_APP_PLATFORM 为通过js判断平台名称的方法，结果分别如下：
			/**
			 * h5，app-plus(nvue下也为app-plus)，mp-weixin，mp-alipay......
			 */
			// #ifdef MP-WEIXIN
			this.checkVersion()
			// #endif
		},
		onShow() {
		    this.$nextTick(async() => {
		    	await this.loadLocation();
		    	this.loadRunEnv()
		    })
		 },
		methods:{
			// 判断手机环境
			loadRunEnv() {
				const res = uni.getSystemInfoSync();
				console.log("手机信息---->" + res.model);
				let model = res.model;
				if (
				  /iphone\sx/i.test(model) ||
				  (/iphone/i.test(model) && /unknown/.test(model)) ||
				  /iphone\s11/i.test(model) ||
				  /iphone\s12/i.test(model)
				) {
				  this.globalData.isIphoneX = true;
				} else {
				  this.globalData.isIphoneX = false;
				}
			},
			// 获取定位信息
		    async loadLocation() {
		      // #ifdef H5
			  // 如果需要h5的话, 这里需要跟原生app通信
		      // this.$store.dispatch('bridge/getToken');
		      // #endif
		      const res = await this.$store.dispatch('location/locationLogLat');
		      const params = {
		        longitude: res.longitude,
		        latitude: res.latitude,
		        that: this
		      };
		      const result = await this.$store.dispatch("address/getAddress", params);
		      let name = result.addressComponent.city.substring(0, 2)
		      let id = ''
		      city.city.map(item => {
		        if (item.list.length) {
		          item.list.map(childer => {
		            if (childer.name == name) {
		              id = childer.id
		            }
		          })
		        }
		      })
		      let obj = {
		        name,
		        id,
		        location: { lng: result.location.lng, lat: result.location.lat },
		        longitude:  res.longitude,
		        latitude: res.latitude,
		        city: result.addressComponent.city,
		        area_code: result.addressComponent.adcode,
		        isLocation: true
		      }
			  this.saveLoadLocation(obj)
		    },
			// 保存定位信息到缓存
			saveLoadLocation(obj) {
				let cityCopy = this.$store.state.vuex_latelyCity
				if (obj.isLocation) {
					cityCopy.splice(0, 1, obj.name)
				} else {
					let isExist = false
					cityCopy.forEach((item) => {
					  if (item === obj.name) {
						isExist = true;
					  }
					})
					cityCopy.splice(1, 0, obj.name)
					if (isExist) {
					  cityCopy = [...new Set(cityCopy)]
					} else {
					  cityCopy = cityCopy.slice(0, -1)
					}
				}
				
				this.$u.vuex('vuex_location', obj);
				this.$u.vuex('vuex_cityCode', obj.area_code);
				this.$u.vuex('vuex_city', obj.city);
				this.$u.vuex('vuex_areaId', obj.id);
				this.$u.vuex('vuex_latelyCity', cityCopy);
			},
			// 检测版本更新
			checkVersion() {
				let res = uni.getAccountInfoSync();
				let envVersion = "";
				switch (res.miniProgram.envVersion) {
					case "develop":
						envVersion = "开发版-";
					break;
					case "trial":
						envVersion = "体验版-";
					break;
					case "realease":
						envVersion = "V";
					break;
				}
				const version = envVersion + (res.miniProgram.version || "暂未发布");
				console.log(version)
				let updateManager = uni.getUpdateManager();
					updateManager.onCheckForUpdate(res => {
					if (res.hasUpdate) {
					  uni.showModal({
						content: `最新版本${version},是否下载最新版本？`,
						showCancel: true,
						cancelColor: "#999999",
						confirmColor: "#25DAB1",
						success: res => {
						  if (res.confirm) {
							updateManager.onUpdateReady(res => {
							  updateManager.applyUpdate();
							});
							updateManager.onUpdateFailed(res => {
							  console.log(res);
							  this.$u.toast('版本更新失败');
							});
						  }
						}
					  });
					} else {
					  console.log('当前已是最新版本')
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "common/demo.scss";
</style>